package db_sports

import (
	"context"
	"github.com/olivere/elastic/v7"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"reflect"
)

/**
 * @Author admin
 * @Description //从ES 中查询足球比分
 * @version: v1.8.0
 * @Date 15:25 2022/7/18
 * @Param
 * @return
 **/
func EsFootballScore(matchIds []interface{}, size int) []*models_sports.FootballScores {
	scoresList := make([]*models_sports.FootballScores, 0)
	if searchResult, err := global.GetEs().Search(_const.ES_MATCH_FOOTBALL_SCORE).Query(elastic.NewTermsQuery(_const.ES_SEARCH_KEYWORD_ID, matchIds...)).Timeout(_const.ES_SEARCH_TIME_OUT).Size(size).Do(context.Background()); err == nil {
		if searchResult.TotalHits() > 0 {
			// 查询结果不为空，则遍历结果
			var score models_sports.FootballScores
			// 通过Each方法，将es结果的json结构转换成struct对象
			for _, item := range searchResult.Each(reflect.TypeOf(score)) {
				if val, ok := item.(models_sports.FootballScores); ok {
					scoresList = append(scoresList, &val)
				}
			}
		}
	}
	return scoresList
}

/**
 * @Author admin
 * @Description //从ES 中查询篮球比分
 * @version: v1.8.0
 * @Date 15:26 2022/7/18
 * @Param
 * @return
 **/
func EsBasketballScore(matchIds []interface{}, size int) []*models_sports.BasketballScore {
	scoresList := make([]*models_sports.BasketballScore, 0)
	if searchResult, err := global.GetEs().Search(_const.ES_MATCH_BASKETBALL_SCORE).Query(elastic.NewTermsQuery(_const.ES_SEARCH_KEYWORD_ID, matchIds...)).Timeout(_const.ES_SEARCH_TIME_OUT).Size(size).Do(context.Background()); err == nil {
		if searchResult.TotalHits() > 0 {
			// 查询结果不为空，则遍历结果
			var score models_sports.BasketballScore
			// 通过Each方法，将es结果的json结构转换成struct对象
			for _, item := range searchResult.Each(reflect.TypeOf(score)) {
				if val, ok := item.(models_sports.BasketballScore); ok {
					scoresList = append(scoresList, &val)
				}
			}
		}
	}
	return scoresList
}
